package vn.aptech.scrumgroup.ui;

import java.util.ArrayList;

import vn.aptech.scrumgroup.R;
import vn.aptech.scrumgroup.activity.ConceptActivity;
import vn.aptech.scrumgroup.model.AbstractBaseModel;
import vn.aptech.scrumgroup.model.BacklogModel;
import vn.aptech.scrumgroup.model.ItemModel;
import vn.aptech.scrumgroup.model.ItemType;
import vn.aptech.scrumgroup.model.ScrumBoard;
import vn.aptech.scrumgroup.model.SprintBurndownModel;
import vn.aptech.scrumgroup.model.ToolArcModel;
import vn.aptech.scrumgroup.resource.Resources;
import vn.aptech.scrumgroup.util.MenuUtil;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import demo.axitdn.event.TouchListener;
import demo.axitdn.physic.Unit;

public class ArtifactView extends View{
	
	ArrayList<AbstractBaseModel> modelList;
	ScrumBoard scrumBoard;
	ToolArcModel Dod;
	ToolArcModel Wa;
	BacklogModel ProductBackLog;
	SprintBurndownModel Burndown;
	private Context context;
	private Intent intent;
	ItemModel item1, item2, item3, item4, item5, item6;
	ArrayList<ItemModel> itemList; 
	ItemModel currentItem;
	
	public ArtifactView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	private void init() {
		// TODO Auto-generated method stub
		itemList = new ArrayList<ItemModel>();
		modelList = new ArrayList<AbstractBaseModel>();
		intent = new Intent();
		
		MenuUtil menu = new MenuUtil(context, Color.BLACK, Color.WHITE);
		modelList.add(menu);
		
		Point tlPoint = new Point((int)(110*Unit.i),(int)(15*Unit.i));
		String colNames[] = new String[]{"Features", "To Do", "Doing", "Verifying", "Done"};
		float cellSize = 40*Unit.i;
		int headerWidth[] = new int[]{2, 2, 2, 2, 2}; 
		int heightByCell = 6;
		scrumBoard = new ScrumBoard(tlPoint, colNames, cellSize, headerWidth, heightByCell, Color.parseColor("#1142AA"), Color.parseColor("#FF5600"), Color.parseColor("#6C8CD5"));
//		scrumBoard.framePaint.setStyle(Style.STROKE);
//		scrumBoard.framePaint.setStrokeWidth(2*Unit.i);
//		scrumBoard.framePaint.setColor(Color.parseColor("#1142AA"));
//		scrumBoard.featurePaint.setColor(Color.parseColor("#6C8CD5"));
//		scrumBoard.featurePaint.setAlpha(150);
//		scrumBoard.textPaint.setStyle(Style.STROKE);
//		scrumBoard.textPaint.setColor(Color.parseColor("#FF5600"));
//		scrumBoard.textPaint.setStrokeWidth(0.8f);
		modelList.add(scrumBoard);
		scrumBoard.addTouchListener(new TouchListener() {
			
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumBoard.getPressState()){
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4&&!item1.isPointOf(x, y)&&!item2.isPointOf(x, y)){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.SPRINT_BACKLOG);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.SPRINT_BACKLOG));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					scrumBoard.setPressState(false);
				}
				scrumBoard.isBrilliant = false;
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(scrumBoard.isPointOfColumn(x, y, 0)){
					this.x = x;
					this.y = y;
					scrumBoard.isBrilliant = true;
					scrumBoard.setPressState(true);
				}
				if(scrumBoard.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					scrumBoard.isBrilliant = true;
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		Dod = new ToolArcModel("Definition of Done", new Point((int)(250*Unit.i), (int)(274*Unit.i)), 10, (int)(70*Unit.i), Color.parseColor("#1142AA"), Color.parseColor("#FF5600"));
		//Dod.toolPaint.setStyle(Style.STROKE);
		//Dod.toolPaint.setColor(Color.parseColor("#1142AA"));
		//Dod.textPaint.setColor(Color.parseColor("#FF5600"));
		//Dod.textPaint.setStyle(Style.STROKE);
//		Dod.textPaint.setTextSize(9);
//		Dod.textPaint.setStrokeWidth(0.8f);
		modelList.add(Dod);
		Dod.addTouchListener(new TouchListener() {
			
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(Dod.getPressState()){
					Dod.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.DEFINITION_OF_DONE);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.DEFINITION_OF_DONE));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					Dod.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(Dod.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					Dod.isBrilliant = true;
					Dod.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		Wa = new ToolArcModel("Work Agreement", new Point((int)(375*Unit.i), (int)(274*Unit.i)), 10, (int)(70*Unit.i), Color.parseColor("#1142AA"), Color.parseColor("#FF5600"));
//		Wa.toolPaint.setStyle(Style.STROKE);
//		Wa.toolPaint.setColor(Color.parseColor("#1142AA"));
//		Wa.textPaint.setColor(Color.parseColor("#FF5600"));
//		Wa.textPaint.setStyle(Style.STROKE);
//		Wa.textPaint.setTextSize(9);
//		Wa.textPaint.setStrokeWidth(0.8f);
		modelList.add(Wa);
		Wa.addTouchListener(new TouchListener() {
			
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(Wa.getPressState()){
					Wa.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.WORK_AGREEMENT);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.WORK_AGREEMENT));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					Wa.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(Wa.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					Wa.isBrilliant = true;
					Wa.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		ProductBackLog = new BacklogModel(10*Unit.i, 10*Unit.i, 65*Unit.i, 100*Unit.i, 15, "", new String[]{"Product","Backlog"}, Color.parseColor("#06276F"), Color.WHITE, Color.parseColor("#FF5600"), Color.WHITE);
//		ProductBackLog.getRectanglePaint().setColor(Color.parseColor("#06276F"));
//		ProductBackLog.getRectanglePaint().setStyle(Style.STROKE);
//		ProductBackLog.getLinePaint().setColor(Color.parseColor("#06276F"));
//		ProductBackLog.getTextPaint().setStyle(Style.STROKE);
//		ProductBackLog.getTextPaint().setStrokeWidth(0.8f);
//		ProductBackLog.getFooterPaint().setStyle(Style.STROKE);
//		ProductBackLog.getFooterPaint().setStrokeWidth(1.0f);
//		ProductBackLog.getFooterPaint().setColor(Color.parseColor("#FF5600"));
//		ProductBackLog.getFooterPaint().setTextSize(10);
		ProductBackLog.addTouchListener(new TouchListener() {
			
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(ProductBackLog.getPressState()){
					ProductBackLog.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.PRODUCT_BACKLOG);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.PRODUCT_BACKLOG));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					ProductBackLog.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(ProductBackLog.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					ProductBackLog.isBrilliant = true;
					ProductBackLog.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		modelList.add(ProductBackLog);
		
		tlPoint  = new Point((int)(110*Unit.i), (int)(330*Unit.i));
		Burndown = new SprintBurndownModel(tlPoint, (int)(100*Unit.i), (int)(60*Unit.i), 12f, Color.RED, Color.RED); 
		modelList.add(Burndown);
		Burndown.addTouchListener(new TouchListener() {
			
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				// TODO Auto-generated method stub
				if(Burndown.getPressState()){
					Burndown.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putString("resourceId", Resources.BURNDOWN);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.BURNDOWN));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					Burndown.setPressState(false);
				}
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(Burndown.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					Burndown.isBrilliant = true;
					Burndown.setPressState(true);
				}
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		
		item1 = new ItemModel(ItemType.USER_STORY);
		scrumBoard.putItem(item1, new Point(0,0));
		item2 = new ItemModel(ItemType.USER_STORY);
		scrumBoard.putItem(item2, new Point(1,0));
		item3 = new ItemModel(ItemType.TASK);
		scrumBoard.putItem(item3, new Point(new Point(2,0)));
		item4 = new ItemModel(ItemType.TASK);
		scrumBoard.putItem(item4, new Point(new Point(3,0)));
		item5 = new ItemModel(ItemType.TASK);
		scrumBoard.putItem(item5, new Point(new Point(2,1)));
		item6 = new ItemModel(ItemType.TASK);
		scrumBoard.putItem(item6, new Point(new Point(2,2)));
		scrumBoard.disableFeaturesColumn(true);
		itemList.add(item1);
		itemList.add(item2);
		itemList.add(item3);
		itemList.add(item4);
		itemList.add(item5);
		itemList.add(item6);
		for (ItemModel item : itemList)
			modelList.add(item);
		addListener(item1);
		addListener(item2);		
		addListener(item3);
		addListener(item4);
		addListener(item5);
		addListener(item6);
		
	}//end method
	private void addListener(final ItemModel item) {
		// TODO Auto-generated method stub
		if (item.type == ItemType.TASK)
		item.addTouchListener(new TouchListener() {
			int x, y;
			@Override
			public boolean onReleased(int x, int y) {
				item.isBrilliant = false;
				float dx = x - this.x;
				float dy = y - this.y;
				float delta = (float) Math.sqrt(dx*dx+dy*dy);
				if (delta<4){
					Bundle productBacklogBundle = new Bundle();
					productBacklogBundle.putInt("resourceId", Resources.TASK);
					productBacklogBundle.putString("title", context.getResources().getString(R.string.TASK));
					intent.replaceExtras(productBacklogBundle);
					intent.setClass(context, ConceptActivity.class);
					context.startActivity(intent);
				}
				item.setPressState(false);
				return false;
			}
			
			@Override
			public boolean onPressed(int x, int y) {
				// TODO Auto-generated method stub
				if(item.isPointOf(x, y)){
					this.x = x;
					this.y = y;
					item.isBrilliant = true;
					item.setPressState(true);
				}//end if
				return false;
			}
			
			@Override
			public boolean onDragged(int x, int y) {
				// TODO Auto-generated method stub
				if(item.getPressState()){
					scrumBoard.moveItemCoordinate(item, x, y);
					return true;
				}
				return false;
			}//end method
		});
		else if (item.type==ItemType.USER_STORY)
			item.addTouchListener(new TouchListener() {
				
				int x, y;
				@Override
				public boolean onReleased(int x, int y) {
					item.isBrilliant = false;
					float dx = x - this.x;
					float dy = y - this.y;
					float delta = (float) Math.sqrt(dx*dx+dy*dy);
					if (delta<4){
						Bundle productBacklogBundle = new Bundle();
						productBacklogBundle.putInt("resourceId", Resources.USER_STORY);
						productBacklogBundle.putString("title", context.getResources().getString(R.string.USER_STORY));
						intent.replaceExtras(productBacklogBundle);
						intent.setClass(context, ConceptActivity.class);
						context.startActivity(intent);
					}
					item.setPressState(false);
					return false;
				}//end method
				
				@Override
				public boolean onPressed(int x, int y) {
					// TODO Auto-generated method stub
					if(item.isPointOf(x, y)){
						this.x = x;
						this.y = y;
						item.isBrilliant = true;
						item.setPressState(true);
					}//end if
					return false;
				}
				
				@Override
				public boolean onDragged(int x, int y) {
					// TODO Auto-generated method stub
					return false;
				}//end method
			});
	}//end method

	@Override
	public void draw(Canvas canvas) {
		// TODO Auto-generated method stub
		canvas.drawColor(Color.WHITE);
		for (AbstractBaseModel model : modelList){
			model.onDraw(canvas);
		}//end for
		super.draw(canvas);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		
		for(AbstractBaseModel model : modelList){
			model.fireTouchEvent(event);
		}
		this.invalidate();
		return true;
	}
}
